//
//  PERealTimeDataCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/25.
//

#import "RHInputRealTimeDataCell.h"
@interface RHInputRealTimeDataCell()




@end
@implementation RHInputRealTimeDataCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    NSNumber *num1 = [NSNumber numberWithFloat:[dic[@"l-pv-L1"]floatValue]];
    NSNumber *num2 = [NSNumber numberWithFloat:[dic[@"P-pv-L1"]floatValue]];
    NSNumber *num3 = [NSNumber numberWithFloat:[dic[@"V-pv-L1"]floatValue]];
    NSNumber *num4 = [NSNumber numberWithFloat:[dic[@"l-pv-L2"]floatValue]];
    NSNumber *num5 = [NSNumber numberWithFloat:[dic[@"P-pv-L2"]floatValue]];
    NSNumber *num6 = [NSNumber numberWithFloat:[dic[@"V-pv-L2"]floatValue]];
    
    QMUILabel *lab1 = [self.contentView viewWithTag:200];
    lab1.text = [NSString stringWithFormat:@"%@A",[num1 decimalPlacesWithoutRoundDownWithDigit:1]];
    
    QMUILabel *lab2 = [self.contentView viewWithTag:201];
    lab2.text = [NSString stringWithFormat:@"%@V",[num2 decimalPlacesWithoutRoundDownWithDigit:1]];
    
    QMUILabel *lab3 = [self.contentView viewWithTag:202];
    lab3.text = [NSString stringWithFormat:@"%@W",[num3 decimalPlacesWithoutRoundDownWithDigit:1]];
    
    QMUILabel *lab4 = [self.contentView viewWithTag:203];
    lab4.text = [NSString stringWithFormat:@"%@A",[num4 decimalPlacesWithoutRoundDownWithDigit:1]];
    
    QMUILabel *lab5 = [self.contentView viewWithTag:204];
    lab5.text = [NSString stringWithFormat:@"%@V",[num5 decimalPlacesWithoutRoundDownWithDigit:1]];
    
    QMUILabel *lab6 = [self.contentView viewWithTag:205];
    lab6.text = [NSString stringWithFormat:@"%@W",[num6 decimalPlacesWithoutRoundDownWithDigit:1]];
    
}


-(void)setupUI
{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    kWeakSelf;
    __block UIView *lastView;
    __block CGFloat margin = 10;
    __block CGFloat itemW = (kUI_WINDOW_W-30)/3;
    NSArray *titles = @[@"I-pv-L1",@"V-pv-L1",@"P-pv-L1",@"I-pv-L2",@"V-pv-L2",@"P-pv-L2"];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [weakSelf itemViewWithIndex:idx title:titles[idx]];
        [bgView addSubview:itemView];
        if(idx == 0){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(0);
                make.width.offset(itemW);
                make.height.offset(itemW*0.8);
            }];
            //
        }else{
            if(idx%3==0){//换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(0);
                    make.top.equalTo(lastView.mas_bottom);
                    make.size.equalTo(lastView);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
                
        lastView = itemView;
        
        //不是最后一行就加上一竖线
        if(idx % 3 != 2){
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"E7E7E7");
            [self addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(lastView).multipliedBy(0.5);
                make.width.offset(1);
                make.centerY.equalTo(lastView);
                make.right.equalTo(lastView);
            }];
        }

    }];
    //封底
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    //加和条横线
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"E7E7E7");
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(1);
        make.centerY.mas_offset(0);
    }];
}


-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title
{
    UIView *bgView = kView;
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.text = title;
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    titleLab.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentCenter;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = [UIFont systemFontOfSize:16];
    [bgView addSubview:contentLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        make.centerY.mas_offset(15);
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.centerY.mas_offset(-15);
    }];
    return bgView;
}

@end
